package org.lewis.woman.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.lewis.common.domain.User;
import org.lewis.common.service.UserService;
import org.lewis.common.utils.WebUtils;

import com.tocersoft.framework.core.annotations.Controller;
import com.tocersoft.framework.core.annotations.Injection;
import com.tocersoft.framework.web.annotations.Mapping;
import com.tocersoft.framework.web.annotations.NameSpace;
import com.tocersoft.framework.web.renderer.Renderer;
import com.tocersoft.framework.web.renderer.TextRenderer;

@Controller
@NameSpace("/admin/user")
public class UserController {
	
	public static final int size=50;
	
	@Injection
	UserService userService;

	@Mapping("/list/$1")
	public Renderer getJsonListByPage(int page) throws JSONException {
		JSONArray json=new JSONArray();
		Map<String, Object> data=new HashMap<String, Object>();
		data.put("name", WebUtils.getParameter("name"));
		data.put("email", WebUtils.getParameter("email"));
		data.put("stime", WebUtils.getParameter("timestart"));
		data.put("etime", WebUtils.getParameter("timeend"));
		List<User> result=userService.queryByPage(page, size,data);
		for (User user : result) {
			JSONObject obj=new JSONObject();
			obj.put("id", user.getId());
			obj.put("name", user.getName());
			obj.put("email", user.getEmail());
			obj.put("password", user.getPassword());
			obj.put("createTime", user.getCreateTime());
			obj.put("status", user.getStatusStr());
			json.put(obj);
		}
		return new TextRenderer(json.toString());
	}
	
	@Mapping("/changestatus/$1/$2")
	public Renderer changStatus(long uid,int status){
		userService.updateUserStatus(uid, status);
		return null;
	}

}
